package com.situ.company.department.service.impl;

import java.util.List;

import com.situ.company.department.dao.DepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.IDepartmentService;
import com.situ.company.employee.dao.EmployeeDao;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.FmtEmpty;

public class DepartmentServiceImpl implements IDepartmentService {

    private DepartmentDao dao = new DepartmentDao();
    private EmployeeDao employeeDao = new EmployeeDao();
    
    @Override
    public String insert(DepartmentModel model) {
        if(selectModel(model)==null) {
            Integer res = dao.insert(model);
            return res == null ? null:res.toString();
        }
        return "repeat";
    }

    @Override
    public String delete(DepartmentModel model) {
        EmployeeModel m1 = new EmployeeModel();
        m1.setCodeDept(model.getCode());
        List<EmployeeModel> list = employeeDao.selectList(m1);
        if(FmtEmpty.isEmpty(list)) {
            Integer res = dao.delete(model);
            return res == null ? null : res.toString(); 
        }
        return "exist";
    }

    @Override
    public String update(DepartmentModel model) {
        return dao.updateActive(model) + "";
    }

    @Override
    public DepartmentModel selectModel(DepartmentModel model) {
        DepartmentModel m0 = new DepartmentModel(model.getCode());
        return dao.selectModel(m0);
    }

    @Override
    public List<DepartmentModel> selectList(DepartmentModel model) {
        String code = model.getCode();
        model.setCode(code == null ? "%%" : "%" + code + "%");
        String name = model.getName();
        model.setName(name == null ? "%%" : "%" + name + "%");
        return dao.selectList(model);
    }

    @Override
    public Integer selectCount(DepartmentModel model) {
        DepartmentModel m0 = new DepartmentModel();
        String code = model.getCode();
        m0.setCode(code == null ? "%%" : "%" + code + "%");
        String name = model.getName();
        m0.setName(name == null ? "%%" : "%" + name + "%");
        return dao.selectCount(m0);
    }
}
